package org.sky.sys.controller;

import org.sky.exception.ServiceException;
import org.sky.security.support.SecurityUser;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.DateUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysOnlineUserController
 * @ProjectName springboot-platform
 * @Description: TODO{在线用户管理}
 * @date 2019/3/13 14:14
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/sys/SysOnlineUser")
public class SysOnlineUserController {
    @Autowired
    private SessionRegistry sessionRegistry;

    /**
     * 打开在线用户页面
     * @return
     */
    @GetMapping(value = "/initSysOnlineUserMngPage")
    public String initSysOnlineUserMngPage() {
        return "sys/onlineuser/onlineusermng";
    }

    /**
     * 查询在线用户
     * @return
     */
    @PostMapping(value = "/selectOnlineUser",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectOnlineUser(){
        List<Object> slist =sessionRegistry.getAllPrincipals();
        List rows = new ArrayList();
        for(int i=0;i<slist.size();i++){
            List<SessionInformation> sessionList = sessionRegistry.getAllSessions(slist.get(i),true);
            SecurityUser user=(SecurityUser)slist.get(i);
            for(SessionInformation t:sessionList){
                if(t.isExpired()){
                    continue;
                }
                Map res = new HashMap();
                res.put("id", Long.toString(user.getId()));
                res.put("userCode", user.getCode());
                res.put("userName", user.getUsername());
                res.put("loginIp",  user.getLoginIp());
                res.put("loginDate", DateUtils.parseDate(user.getLoginDate(),null));
                res.put("sessionId", t.getSessionId());
                res.put("lastRequest", DateUtils.parseDate(t.getLastRequest(),null));
                rows.add(res);
            }
        }
        PageListData pageData = new PageListData();
        pageData.setTotal(slist.size());
        pageData.setRows(rows);
        return JsonUtils.obj2json(pageData);
    }
    @PostMapping(value = "/kickOut",produces = "application/json;charset=UTF-8")
    public @ResponseBody String kickOut(
            HttpServletRequest request){
        Result result = null;
        try {
            String kickOutRows=request.getParameter("kickOutRows");
            List<Map> kickList=JsonUtils.json2list(kickOutRows, Map.class);
            for(Map kick:kickList){
                String userCode = (String)kick.get("userCode");
                List<Object> objects = this.sessionRegistry.getAllPrincipals();
                for (Object o : objects) {
                    SecurityUser user = (SecurityUser) o;
                    if (user.getCode().equals(userCode)) {
                        List<SessionInformation> sis = this.sessionRegistry
                                .getAllSessions(o, false);
                        if (sis != null) {
                            for (SessionInformation si : sis) {
                                si.expireNow();
                                //sessionRegistry.removeSessionInformation((String)kick.get("sessionId"));
                            }
                        }
                    }
                }
            }
            result = Result.success("强制下线成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("强制下线失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
}
